using MallSystembackend.Api.Repository;
using MallSystembackend.Api.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MallSystembackend.Api.ParamModel;
using System.Linq;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ShoppingTrolleyController : ControllerBase
    {
        public IRepository<ShoppingTrolley> _shoppingTrolleyRepository;
        public readonly IConfiguration _configuration;

        public ShoppingTrolleyController(IRepository<ShoppingTrolley> shoppingTrolleyRepository,
        IConfiguration configuration)
        {
            _configuration = configuration;
            _shoppingTrolleyRepository = shoppingTrolleyRepository;
        }
        //加入购物车
        [HttpPost]
        public ActionResult AddShoppingTrolley(ParamShoppingTrolley model)
        {
            var AddShoppingTrolley = new ShoppingTrolley
            {
                UsersId = model.UsersId,
                CommodityId = model.CommodityId,
                IsActived=true,
                IsDeleted=false
            };
            _shoppingTrolleyRepository.Insert(AddShoppingTrolley);
            return Ok(new
            {
                Code = 1000,
                Data = AddShoppingTrolley,
                Msg = "添加成功"
            });
        }
        //移出购物车
        [HttpDelete("{id}")]
        public ActionResult DeletedShopping(int id)
        {
            var shoppingTrolley = _shoppingTrolleyRepository.GetById(id);
            if (shoppingTrolley != null)
            {
                shoppingTrolley.IsDeleted = true;
                _shoppingTrolleyRepository.Update(shoppingTrolley);
                return Ok(new
                {
                    Code = 1000,
                    Data = shoppingTrolley,
                    Msg = "删除成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = shoppingTrolley,
                Msg = "删除失败"
            });
        }
        //根据用户id获得购物车内的信息
        [HttpGet("{id}")]
        public ActionResult GetShop(int id)
        {
            var shoppingTrolley = _shoppingTrolleyRepository.Table;
            var byUserIdGet = shoppingTrolley.Where(x => x.UsersId == id).Where(x => x.IsDeleted == false);
            return Ok(new
            {
                Code = 1000,
                Data = shoppingTrolley,
                Msg = "获得成功"
            });
        }
    }
}